package com.kh.first;

//import com.kh.first.task.EndListener;
import org.activiti.engine.*;
import org.activiti.engine.history.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.DelegationState;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
// 让 JUnit 运行 Spring 的测试环境， 获得 Spring 环境的上下文的支持
@RunWith(SpringRunner.class)
public class First {
	@Autowired
	private ProcessEngine processEngine;

	@Autowired
	RepositoryService repositoryService;

//	@Autowired
//	EndListener endListener;

	@Test
	public void deploy() throws FileNotFoundException {
		String file = "D:\\work\\java-web\\kh-repair\\src\\test\\resources\\processes\\test.bpmn";

//        processEngine.getRepositoryService().createDeployment()
//                .name("ccccc")
//                .addInputStream(file, new FileInputStream(file)).deploy();
		processEngine.getRepositoryService().createDeployment()//
				.addClasspathResource("processes/computer-apply.bpmn")//
				.addClasspathResource("processes/computer-apply.png")//
				.deploy();//
		ProcessDefinition processDefinition = processEngine.getRepositoryService().createProcessDefinitionQuery()
				.processDefinitionKey("computer_apply").latestVersion().singleResult();

		if (processDefinition != null) {
			String engineVersion = processDefinition.getEngineVersion();
			String deploymentId = processDefinition.getDeploymentId();
			int version = processDefinition.getVersion();
			String id = processDefinition.getId();
			String key = processDefinition.getKey();
			System.out.printf("%s::%s::%d::%s::%s\n", engineVersion, deploymentId, version, id, key);
		}
	}

	@Test
	public void commit() {
		RuntimeService runtimeService = processEngine.getRuntimeService();
//        runtimeService.startProcessInstanceById("myProcess_1:1:4");
		Map<String, Object> var = new HashMap<>();
		var.put("user", "tt");
		ProcessInstance leaveBill = runtimeService.startProcessInstanceByKey("leaveBill", var);
		String processDefinitionId = leaveBill.getId();
		System.out.println(":::---" + processDefinitionId);// 47501 45001
	}

	@Test
	public void complete() {

		TaskService taskService = processEngine.getTaskService();
		Task test = taskService.newTask("test");
		DelegationState delegationState = test.getDelegationState();
		TaskQuery taskQuery = taskService.createTaskQuery();
		List<Task> zs = taskQuery.taskCandidateOrAssigned("zs").list();
		Map<String, Object> var = new HashMap<>();
		var.put("status", "1");
		taskService.complete("50003", var);

		for (Task z : zs) {
//            taskService.complete(z.getId());
		}
	}

	@Test
	public void history() {
		HistoryService historyService = processEngine.getHistoryService();
		List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
				.processInstanceId("45001").list();

		HistoricVariableInstanceQuery historicVariableInstanceQuery = historyService
				.createHistoricVariableInstanceQuery();
		List<HistoricVariableInstance> list1 = historicVariableInstanceQuery.processInstanceId("45001").list();
		for (HistoricVariableInstance historicVariableInstance : list1) {
			String variableName = historicVariableInstance.getVariableName();
			Object value = historicVariableInstance.getValue();
			System.out.printf(variableName + "::" + value + "\n");
		}
		for (HistoricActivityInstance historicDetail : list) {
			String id = historicDetail.getId();
			System.out.println("--" + id);
			String processVariables = historicDetail.getActivityName();
			System.out.println("--" + processVariables);
		}
	}
}
